using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using Microsoft.DirectX;

namespace Scenario_Editor.WR2TerrainLoader
{
    class WR2Vertex
    {
        static int staticID;

        private long positionInfile;
        int id;

        float positionX;
        float positionY;
        float positionZ;
        byte[] normal = new byte[4];
        float textureU;
        float textureV;
        byte[] blendcolor;// = new byte[4];
        byte[] color2 = new byte[4];

        internal WR2Vertex(BinaryReader br)
        {
            positionInfile = br.BaseStream.Position;
            this.id = WR2Vertex.ID;
            positionX = br.ReadSingle();// / 10;
            positionY = br.ReadSingle();// / 10;
            positionZ = br.ReadSingle();// / 10;
            normal = br.ReadBytes(4);
            textureU = br.ReadSingle();
            textureV = br.ReadSingle();
            blendcolor = br.ReadBytes(4); ;// br.ReadInt32();// br.ReadBytes(4);
            color2 = br.ReadBytes(4);
            //BlendR,BlendG,BlendB,Shadow,B,G,R,A
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Vertex: " + this.id.ToString() + " at Position: " + this.positionInfile.ToString());
            sb.AppendLine("PositionX: " + this.positionX.ToString());
            sb.AppendLine("PositionY: " + this.positionY.ToString());
            sb.AppendLine("PositionZ: " + this.positionZ.ToString());
            sb.AppendLine("color:     " + ByteToString(this.normal));//.ToString());
            sb.AppendLine("U:         " + this.textureU.ToString());
            sb.AppendLine("V:         " + this.textureV.ToString());
            sb.AppendLine("unknown2:  " + ByteToString(this.blendcolor));//.ToString());//.ToString());
            //sb.AppendLine("unknown3:  " + this.unknown3.ToString());
            return sb.ToString();
        }


        public static string ByteToString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                sb.Append(bytes[i].ToString() + " ");

            }
            return sb.ToString();
        }

        public static int ID
        {
            get
            {
                WR2Vertex.staticID++;
                return WR2Vertex.staticID;
            }
            set
            {
                ID = value;
            }
        }
        
        public Color Color
        {
            get
            {
               return Color.FromArgb(255, blendcolor[0], blendcolor[1], blendcolor[2]);// blendcolor;
                 //return Color.FromArgb(color2[3], color2[2], color2[1], color2[0]);// color2;
            }
        }

        private static bool test = true;
        internal void Save(BinaryWriter bw)
        {
            //Yellow Texture 1 und 3;

            //Single16 test = new Single16(50);

            
            bw.Write(positionX);// / 10;
             bw.Write(positionY);// / 10;
             bw.Write(positionZ);// / 10;
             bw.Write(normal);
             bw.Write(textureU);
             bw.Write(textureV);

             //bw.Write(blendcolor); ;// br.ReadInt32();// br.ReadBytes(4);
             if (WR2Vertex.test)
             {
                 WR2Vertex.test = false;
                 bw.Write((byte)0);//Red
                 bw.Write((byte)0);//Green
                 bw.Write((byte)0);//Blau
                 bw.Write((byte)0);//shadow
             }
             else
             {
                 WR2Vertex.test = true;
                 bw.Write((byte)0);//Red
                 bw.Write((byte)0);//Green
                 bw.Write((byte)0);//Blau
                 bw.Write((byte)200);//shadow
             }


             //bw.Write(color2);
             //BlendR,BlendG,BlendB,Shadow,B,G,R,A
             if (WR2Vertex.test)
             {
                 //WR2Vertex.test = false;
                 bw.Write((byte)20);//Red
                 bw.Write((byte)20);//Green
                 bw.Write((byte)20);//Blau
                 bw.Write((byte)255);//shadow
             }
             else
             {
                 //WR2Vertex.test = true;
                 bw.Write((byte)20);//Red
                 bw.Write((byte)20);//Green
                 bw.Write((byte)20);//Blau
                 bw.Write((byte)255);//shadow
             }
        }
    }
}
